package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.dto.PageDto;
import com.itheima.ydd.dto.YddArticleDto;
import com.itheima.ydd.entity.Result;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.exception.ConsumerException;
import com.itheima.ydd.mapper.YddArticleMapper;
import com.itheima.ydd.service.YddArticleService;

import com.itheima.ydd.service.YddCateService;
import com.itheima.ydd.service.YddTagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class YddArticleServiceImpl extends ServiceImpl<YddArticleMapper, YddArticle> implements YddArticleService {
    @Autowired
    private YddCateService yddCateService;

    @Override
    public ResponseEntity<Result> findPages(String startTime, String endTime, String title, String cateid, Integer page, Integer limit) {
        //设置分页参数
        PageDto<YddArticle> pg = new PageDto<>(page, limit);
        //构造条件构造器-条件对象
        LambdaQueryWrapper<YddArticle> queryWrapper = new LambdaQueryWrapper<>();
        //判断title是否为空    null都是false
        queryWrapper.like(StringUtils.hasText(title), YddArticle::getTitle, title);
        //判断有无起始和终止时间
        queryWrapper.between(!StringUtils.isEmpty(startTime), YddArticle::getCreate_date, startTime, endTime);
        //判断cateid是否为空
        queryWrapper.eq(StringUtils.hasText(cateid), YddArticle::getCateid, cateid);
        //添加排序规则
        queryWrapper.orderByDesc(YddArticle::getUpdate_date);

        //分页(条件)查询
        page(pg, queryWrapper);

        PageDto<YddArticleDto> pgDto = new PageDto<>();

        BeanUtils.copyProperties(pg, pgDto, "records");
        List<YddArticle> yddArticleList = pg.getdata();
        List<YddArticleDto> list = new ArrayList<>();
        if (!ObjectUtils.isEmpty(yddArticleList)) {
            for (YddArticle yddArticle : yddArticleList) {
                YddCate yddCate = yddCateService.getById(yddArticle.getCateid());
                YddArticleDto dto = new YddArticleDto();
                BeanUtils.copyProperties(yddArticle, dto);
                dto.setCatename(yddCate.getCatename());
                list.add(dto);
            }
        }
        pgDto.setRecords(list);
        return ResponseEntity.ok(new Result(0, "", pgDto));
    }

    @Override
    public ResponseEntity<Result> deleteById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            throw new ConsumerException("请选择要删除的数据");
        }
        removeById(id);
        return ResponseEntity.ok(new Result(0, ""));
    }

    @Override
    public ResponseEntity<Result> add(YddArticle yddArticle) {
        YddArticle article = getOne(new LambdaQueryWrapper<YddArticle>().eq(YddArticle::getTitle, yddArticle.getTitle()));
        if (!ObjectUtils.isEmpty(article)) {
            throw new ConsumerException("标题已存在，请重新输入");
        }
        yddArticle.setUpdate_date(yddArticle.getCreate_date());
        save(yddArticle);
        return ResponseEntity.ok(new Result(0, "", yddArticle));
    }

    @Override
    public ResponseEntity<Result> selectById(Integer id) {
        if (ObjectUtils.isEmpty(id)){
            throw new ConsumerException("请选择要修改的文章");
        }
        YddArticle yddArticle = getById(id);
        YddArticleDto dto = new YddArticleDto();
        if (!ObjectUtils.isEmpty(yddArticle)) {
            BeanUtils.copyProperties(yddArticle, dto);
            //设置分类名称
            LambdaQueryWrapper<YddCate> yddCateLambdaQueryWrapper = new LambdaQueryWrapper<>();
            yddCateLambdaQueryWrapper.eq(YddCate::getId, yddArticle.getCateid());
            YddCate cate = yddCateService.getOne(yddCateLambdaQueryWrapper);
            dto.setCatename(cate.getCatename());
        }
        return ResponseEntity.ok(new Result(0, "", dto));
    }

    @Override
    public ResponseEntity<Result> updateArticle(YddArticle yddArticle) {
        YddArticle article = getOne(new LambdaQueryWrapper<YddArticle>().eq(YddArticle::getTitle, yddArticle.getTitle()));
        if (!ObjectUtils.isEmpty(article)) {
            if (!article.getId().equals(yddArticle.getId())) {
                throw new ConsumerException("标题已存在，请重新输入");
            } else {
                yddArticle.setUpdate_date(new Date().getTime());
                updateById(yddArticle);
                return ResponseEntity.ok(new Result(0, "", yddArticle));
            }
        }
        yddArticle.setUpdate_date(new Date().getTime());
        updateById(yddArticle);
        return ResponseEntity.ok(new Result(0, "", yddArticle));
    }
}
